package com.bawei.module_mine.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.mvi.base.BaseViewModel
import com.bawei.module_mine.intent.ConfirmOrderIntent
import com.bawei.module_mine.model.service.OrderService
import com.bawei.module_mine.state.ConfirmOrderState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch
import android.util.Log

class ConfirmOrderViewModel : BaseViewModel<ConfirmOrderIntent>() {
    private val service by lazy { OrderService() }
    private val _state = MutableStateFlow<ConfirmOrderState>(ConfirmOrderState.Init)
    val state: StateFlow<ConfirmOrderState> get() = _state

    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect { intent ->
                when (intent) {
                    is ConfirmOrderIntent.GetCouponMaxDis -> {
                        _state.value = ConfirmOrderState.Loading
                        try {
                            val res = service.getCouponMaxDis(intent.req)
                            if (res.code == 200) {
                                _state.value = ConfirmOrderState.CouponMaxDis(res.data)
                            } else {
                                _state.value = ConfirmOrderState.Failed("获取优惠信息失败，请稍后重试")
                            }
                        } catch (e: Exception) {
                            Log.e("OrderDebug", "最大优惠接口异常", e)
                            _state.value = ConfirmOrderState.Failed("网络连接异常，请检查网络后重试")
                        }
                    }
                    is ConfirmOrderIntent.GetOrderCashDetail -> {
                        _state.value = ConfirmOrderState.Loading
                        try {
                            val res = service.getOrderCashDetail(intent.req)
                            if (res.code == 200) {
                                _state.value = ConfirmOrderState.OrderCashDetail(res.data)
                            } else {
                                _state.value = ConfirmOrderState.Failed("获取支付方式失败，请稍后重试")
                            }
                        } catch (e: Exception) {
                            Log.e("OrderDebug", "支付方式接口异常", e)
                            _state.value = ConfirmOrderState.Failed("网络连接异常，请检查网络后重试")
                        }
                    }
                    is ConfirmOrderIntent.GetPayCostTrial -> {
                        _state.value = ConfirmOrderState.Loading
                        try {
                            val res = service.getPayCostTrial(intent.req)
                            if (res.code == 200) {
                                _state.value = ConfirmOrderState.PayCostTrial(res.data)
                            } else {
                                _state.value = ConfirmOrderState.Failed("获取手续费信息失败，请稍后重试")
                            }
                        } catch (e: Exception) {
                            Log.e("OrderDebug", "手续费接口异常", e)
                            _state.value = ConfirmOrderState.Failed("网络连接异常，请检查网络后重试")
                        }
                    }
                    is ConfirmOrderIntent.GetOrderCouponSelect -> {
                        _state.value = ConfirmOrderState.Loading
                        try {
                            val res = service.getOrderCouponSelect(intent.req)
                            if (res.code == 200) {
                                _state.value = ConfirmOrderState.OrderCouponSelect(res.data)
                            } else {
                                _state.value = ConfirmOrderState.Failed("获取推荐优惠券失败，请稍后重试")
                            }
                        } catch (e: Exception) {
                            Log.e("OrderDebug", "推荐优惠券接口异常", e)
                            _state.value = ConfirmOrderState.Failed("网络连接异常，请检查网络后重试")
                        }
                    }
                    is ConfirmOrderIntent.GetShippingCost -> {
                        _state.value = ConfirmOrderState.Loading
                        try {
                            val res = service.getShippingCost(intent.req)
                            if (res.code == 200) {
                                _state.value = ConfirmOrderState.ShippingCost(res.data)
                            } else {
                                _state.value = ConfirmOrderState.Failed("获取运费信息失败，请稍后重试")
                            }
                        } catch (e: Exception) {
                            Log.e("OrderDebug", "运费接口异常", e)
                            _state.value = ConfirmOrderState.Failed("网络连接异常，请检查网络后重试")
                        }
                    }
                }
            }
        }
    }
} 